package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.config.PageInfo;
import cn.wolfcode.rbac.config.QueryObject;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.mapper.RoleMapper;
import cn.wolfcode.rbac.mapper.RolePermissionMapper;
import cn.wolfcode.rbac.model.PermissionDO;
import cn.wolfcode.rbac.model.RoleDO;
import cn.wolfcode.rbac.model.RolePermissionDO;
import cn.wolfcode.rbac.request.RoleRequest;
import cn.wolfcode.rbac.service.RoleService;
import cn.wolfcode.rbac.vo.DepartmentVO;
import cn.wolfcode.rbac.vo.RoleVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Lu.F
 * @version 1.0
 * @Description TODO
 * @date 2022/5/20 14:28
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;
    /**
     * 查看角色权限所有信息
     * @return
     */
    @Override
    public List<RoleDO> listAll() {
        return roleMapper.selectList(null);
    }

    /**
     * 分页查询角色
     * @return
     */
    @Override
    public PageInfo<RoleDO> pageDepartment(QueryObject qo) {

        Page<RoleDO> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        IPage<RoleDO> permissionDOPage = roleMapper.selectPage(page, null);
        PageInfo<RoleDO> doPageInfo = new PageInfo<>();
        doPageInfo.setPage(qo.getCurrentPage());
        doPageInfo.setTotalPage(permissionDOPage.getPages());
        doPageInfo.setSize(qo.getPageSize());
        doPageInfo.setTotalRecords(permissionDOPage.getTotal());
        doPageInfo.setCurrentData(permissionDOPage.getRecords());
        return doPageInfo;
    }


    /**
     * 根据id查找角色
     * @param id
     * @return
     */
    @Override
    public RoleVO get(Long id) {
        RoleVO roleVO = new RoleVO();
        RoleDO roleDO = roleMapper.selectById(id);
        BeanUtils.copyProperties(roleDO,roleVO);
        List<PermissionDO> permissionDOList = new ArrayList<>();
        List<RolePermissionDO> rolePermissionDOS = rolePermissionMapper.selectList(new QueryWrapper<RolePermissionDO>().eq("role_id", id));
        for (RolePermissionDO obj : rolePermissionDOS){
            PermissionDO permissionDO = permissionMapper.selectById(obj.getPermissionId());
            permissionDOList.add(permissionDO);
        }
        roleVO.setPermissions(permissionDOList);
        return roleVO;
    }

    @Override
    public void saveOrUpdate(RoleRequest roleRequest, List<Long> permissionIds) {
        Long roleRequestId = roleRequest.getId();
        RoleDO roleDO = roleMapper.selectOne(new QueryWrapper<RoleDO>().eq("id",roleRequestId));
        if (roleDO != null){
            // 不为空则进行更新
            BeanUtils.copyProperties(roleRequest,roleDO);
            roleMapper.update(roleDO,new QueryWrapper<RoleDO>().eq("id",roleRequestId));
        }else {
            RoleDO roleDO1 = new RoleDO();
            BeanUtils.copyProperties(roleRequest,roleDO1);
            roleMapper.insert(roleDO1);
        }

        rolePermissionMapper.delete(new QueryWrapper<RolePermissionDO>().eq("role_id",roleRequestId));
        if (roleRequestId == null) {
            RoleDO aDo = roleMapper.selectOne(new QueryWrapper<RoleDO>().eq("name", roleRequest.getName()));
            roleRequestId = aDo.getId();
        }

        if(permissionIds != null && permissionIds.size() != 0) {
            RolePermissionDO rolePermissionDO = new RolePermissionDO();
            rolePermissionDO.setRoleId(roleRequestId);
            for (Long permissionId : permissionIds) {
                rolePermissionDO.setPermissionId(permissionId);
                rolePermissionMapper.insert(rolePermissionDO);
            }
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        roleMapper.delete(new QueryWrapper<RoleDO>().eq("id",id));
    }
}
